/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mapping;
import java.sql.ResultSet;
import mapping.SqlLib;
import entreprise.Session;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Quoc-Khai
 */
public class DBSession {
    public Session getSession(int sessionID) throws SQLException{
        Session iSession = new Session();
        String selectQuery;
        selectQuery = "SELECT * FROM session WHERE sessionID='"+sessionID+"'";
        
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);
            
            while(sqlResultSet.next()) {
               iSession.setSessionID(sqlResultSet.getString("sessionID")); 
               iSession.setSessionDate(sqlResultSet.getDate("sessionDate")); 
               iSession.setDoctor(sqlResultSet.getString("doctor")); 
               iSession.setRequire(sqlResultSet.getString("require"));   
               iSession.setDiagnostic(sqlResultSet.getString("diagnostic"));   
            }
        } else {
            System.out.println("Session not found!");
        } 
        return iSession;
    }
    public boolean createSession(Session session){
        //TODO;
        java.sql.Date sqlDate = new java.sql.Date(session.getSessionDate().getTime());
         String insertQuery;
        insertQuery = "INSERT INTO session (sessionDate, doctor, `require`, diagnostic) VALUES ('" + sqlDate + "','" + session.getDoctor()+ "','" + session.getRequire()+ "','" + session.getDiagnostic()+ "')";
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(insertQuery);
            System.out.println("Insert Session success!");
            return true;
        }
        return false;
    }
    public boolean updateSession(Session session){
        //TODO
        java.sql.Date sqlDate = new java.sql.Date(session.getSessionDate().getTime());
        String updateQuery;
        updateQuery = "UPDATE session SET sessionDate='" + sqlDate+ 
                "', doctor='" + session.getDoctor()+
                "',`require`='" + session.getRequire()+ 
                "',diagnostic='" + session.getDiagnostic()+ 
                "' WHERE sessionID='"+session.getSessionID()+"'";
        
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(updateQuery);
            System.out.println("Update Session success!");
            return true;
        }
        return false;
    }
    public boolean deleteSession(int sessionID){
        //TODO
        String deleteQuery;
        deleteQuery = "DELETE FROM session WHERE sessionID='"+sessionID+"'"; 
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(deleteQuery);
            System.out.println("Delete Session success!");
            return true;
        }
        return false;
    }    
    public List<Session> getSessions(String conditions){
        List<Session> sessions=null;
        //TODO
        return sessions;
    }    
}
